package com.shm.alibaba;

import java.util.Scanner;

/**
 * @author SHM
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int t = Integer.parseInt(s);
        for (int j = 0; j < t; j++) {
            String str = input.nextLine();
            String[] ss = str.split(" ");
            String s1 = ss[0];
            String s2 = ss[1];
            char[] chars1 = s1.toCharArray();
            char[] chars2 = s2.toCharArray();
            boolean flag = false;
            int index = 0;
            int start = 0;
            for (int k = 0; k < chars2.length; k++) {
                for (int i = k; i < chars1.length; i++) {
                    if (chars1[i] > chars2[k]) {
                        flag = true;
                        index = i;
                        start = k;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }

            if (!flag) {
                System.out.println(s2);
            } else if (index == start) {
                System.out.println(s1);
            } else {
                char temp = chars1[start];
                chars1[start] = chars1[index];
                chars1[index] = temp;
                String string = new String(chars1);
                System.out.println(string);
            }
        }
        input.close();
    }
}
